package com.rhtchr.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rhtchr.util.domain.DictionaryGroup;
import com.rhtchr.util.domain.DictionaryItem;

public class GenericDaoHibernateImpl<T, PK extends Serializable> extends HibernateDaoSupport {

	private Class<T> persistentClass;
	private Map<String, List<DictionaryItem>> dictionaryMap = new HashMap<String, List<DictionaryItem>>();
	final String groupHql = "From DictionaryGroup dg";
	final String itemByGroupIdHql = "From DictionaryItem di where di.groupId = ?";
	final String itemByGroupIdAndParentIdHql = "From DictionaryItem di where di.groupId = ? and di.parentId = ?";
	final String itemByGroupNameHql = "From DictionaryItem di where di.groupName = ? ";
	final String itemByGroupNameAndParentIdHql = "From DictionaryItem di where di.groupName = ? and di.parentCode = ?";
	private Map<Integer, List<DictionaryItem>> cityMap = new HashMap<Integer, List<DictionaryItem>>();

	private List<DictionaryItem> provinceList = new ArrayList<DictionaryItem>();

	public GenericDaoHibernateImpl(Class<T> t) {
		this.persistentClass = t;
	}

	public Class<T> getPersistentClass() {
		return persistentClass;
	}

	public void delete(T persistentObject) {
		this.getHibernateTemplate().delete(persistentObject);
	}

	@SuppressWarnings("unchecked")
	public T find(PK id) {
		return (T) this.getHibernateTemplate().get(getPersistentClass(), id);
	}

	@SuppressWarnings("unchecked")
	public T find(T object) {
		return (T) this.getHibernateTemplate().findByExample(object);
	}

	@SuppressWarnings("unchecked")
	public PK save(T newInstance) {
		return (PK) this.getHibernateTemplate().save(newInstance);
	}

	public void update(T transientObject) {
		this.getHibernateTemplate().update(transientObject);
	}

	@SuppressWarnings("unchecked")
	public List<DictionaryGroup> readDictionaryGroup() {
		List<DictionaryGroup> list = new ArrayList<DictionaryGroup>();
		try {
			list = getHibernateTemplate().find(groupHql);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	public List<DictionaryItem> readDictionaryItem(DictionaryGroup group) {
		return readDictionaryItem(group.getId());
	}

	@SuppressWarnings("unchecked")
	public List<DictionaryItem> readDictionaryItem(long groupId) {
		List<DictionaryItem> list = new ArrayList<DictionaryItem>();
		try {
			list = getHibernateTemplate().find(itemByGroupIdHql, groupId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<DictionaryItem> readDictionaryItem(long groupId, long parentId) {
		Object[] params = new Object[] { groupId, parentId };
		List<DictionaryItem> list = new ArrayList<DictionaryItem>();
		List<DictionaryItem> tmp = dictionaryMap.get("" + groupId + parentId);
		if (null != tmp && tmp.size() > 0) {
			return tmp;
		}
		try {
			list = getHibernateTemplate().find(itemByGroupIdAndParentIdHql, params);
			if (null != list && list.size() > 0) {
				dictionaryMap.put("" + groupId + parentId, list);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<DictionaryItem> readDictionaryItem(String groupName) {
		List<DictionaryItem> tmp = dictionaryMap.get(groupName);
		if (null != tmp && tmp.size() > 0) {
			return tmp;
		}
		List<DictionaryItem> list = new ArrayList<DictionaryItem>();
		try {
			list = getHibernateTemplate().find(itemByGroupNameHql, groupName);
			if (null != list && list.size() > 0) {
				dictionaryMap.put(groupName, list);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	public List<DictionaryItem> readDictionaryItem(String groupName, long parentCode) {
		Object[] params = new Object[] { groupName, parentCode };
		List<DictionaryItem> list = new ArrayList<DictionaryItem>();
		try {
			list = getHibernateTemplate().find(itemByGroupNameAndParentIdHql, params);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	public Map<String, List<DictionaryItem>> getDictionaryMap() {
		return dictionaryMap;
	}

	public void setDictionaryMap(Map<String, List<DictionaryItem>> dictionaryMap) {
		this.dictionaryMap = dictionaryMap;
	}

	/**
	 * 读取宝宝信息。 1-宝宝已经出生。 2-正在怀孕中 3-准备怀孕 4-其他
	 * 
	 * @return 宝宝当前的状态信息。
	 */
	public List<DictionaryItem> getBabyStatusList() {
		return readDictionaryItem("baby_status");
	}

	/**
	 * 读取所有的城市列表信息。
	 * 
	 * @return 返回所有的城市列表信息。
	 */
	public Map<Integer, List<DictionaryItem>> getCityMap() {
		if (cityMap.size() > 0) {
			return cityMap;
		}
		List<DictionaryItem> tmp = readDictionaryItem("province");
		for (DictionaryItem dictionaryItem : tmp) {
			cityMap.put(dictionaryItem.getCode(), new ArrayList<DictionaryItem>());
		}
		List<DictionaryItem> readDictionaryItem = readDictionaryItem("city");
		for (DictionaryItem dictionaryItem : readDictionaryItem) {
			List<DictionaryItem> list = cityMap.get(dictionaryItem.getParentCode());
			list.add(dictionaryItem);
		}
		return cityMap;
	}

	public List<DictionaryItem> getEducationLevelList() {
		return readDictionaryItem("education_level");
	}

	/**
	 * 读取家庭年收入水平信息
	 * 
	 * @return 家庭年收入水平信息列表
	 */
	public List<DictionaryItem> getFamilyIncomeLevelList() {
		return readDictionaryItem("family_income");
	}

	public List<DictionaryItem> getGenderList() {
		return readDictionaryItem("gender");
	}

	public List<DictionaryItem> getProvinceList() {
		if (provinceList.size() > 0) {
			return provinceList;
		}
		provinceList = readDictionaryItem("province");
		return provinceList;
	}
}
